/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hermit.forum.tables;

import hermit.forum.api.ForumCore;
import java.sql.Connection;
import java.sql.Types;
import java.util.LinkedHashMap;
import java.util.List;
import hermit.forum.db.Table;
import hermit.forum.db.TableObject;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Redin Pavel (hermit)
 */
public class Post  extends Table {
    public int id=0;
    public String post="";
    public long date=System.currentTimeMillis();
    public String author_id="";
    public int topic_id=0;
    public int reputation=0;
    public int state=ForumCore.STATE_NORMAL;

    public Post(){
        init();
    }


    public Post(int _id, String _post, Long _date, String _author_id, int _topic_id, int _reputation, int _state){
            id=_id;
            post=_post;
            date=_date;
            author_id=_author_id;
            topic_id=_topic_id;
            reputation=_reputation;
            state=_state;
            init();
    }

    public void init(){
        name="posts";
        structure = new LinkedHashMap();
        structure.put("id", new TableObject(Types.INTEGER,id));
        structure.put("post", new TableObject(Types.VARCHAR,post));
        structure.put("date", new TableObject(Types.TIMESTAMP,date));
        structure.put("author_id", new TableObject(Types.VARCHAR,author_id));
        structure.put("topic_id", new TableObject(Types.INTEGER,topic_id));
        structure.put("reputation", new TableObject(Types.INTEGER,reputation));
        structure.put("state", new TableObject(Types.INTEGER,state));
    }

    @Override
    public String getTableName(){
       return this.name;
    }

    @Override
    public LinkedHashMap<String, TableObject> getStructure() {
        return structure;
    }

    @Override
    public Table getTableObject(Connection db, String q){
        Post p = new Post();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
            stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rSet = stmt.executeQuery(q);
            rSet.next();
            p.id = rSet.getInt(1);
            p.post = rSet.getString(2);
            p.date = rSet.getTimestamp(3).getTime();
            p.author_id = rSet.getString(4);
            p.topic_id = rSet.getInt(5);
            p.reputation = rSet.getInt(6);
            p.state = rSet.getInt(7);
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        return p;
    }


    @Override
    public ArrayList<Table> getTableList(Connection db, String q){
        ArrayList<Table> list = new ArrayList<Table>();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
        	stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        	rSet = stmt.executeQuery(q);
            while(rSet.next()) {
            Post p = new Post();
            p.id = rSet.getInt(1);
            p.post = rSet.getString(2);
            p.date = rSet.getTimestamp(3).getTime();
            p.author_id = rSet.getString(4);
            p.topic_id = rSet.getInt(5);
            p.reputation = rSet.getInt(6);
            p.state = rSet.getInt(7);
                list.add(p);
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        list.trimToSize();
        return list;
    }

 
}
